package com.woniuxy.woniuhis.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.woniuxy.woniuhis.entity.MedicineCost;
import com.woniuxy.woniuhis.exception.MedicineServiceException;
import com.woniuxy.woniuhis.service.MedicineCostService;
import com.woniuxy.woniuhis.util.ResponseInfo;

@Controller
public class MedicineCostController {

    @Autowired
    MedicineCostService medicineCostService;
    
    
	/**
     * @param req
     * @return
     * 删除药物的controllerCost方法
     */
    @RequestMapping("deleteMedicineCost")
    @ResponseBody
    public ModelAndView deleteMedicineController(HttpServletRequest req) {
    	
    	String id = req.getParameter("id");
    	System.out.println(id);
    	
		
    	try {
    		
        	MedicineCost medicineCost = new MedicineCost();
        	if (id!=null&&id!="") {
        		medicineCost.setMediId(id);
			}else {
				throw new NullPointerException();
			}
        	
    		
    		
    		medicineCostService.delMedicineCostByMediID(medicineCost);
        	
        	ResponseInfo responseInfo = new ResponseInfo();
        	responseInfo.setMsg("删除药品成功");
        	responseInfo.setState(200);
        	
        	ModelAndView mv = new ModelAndView();
        	mv.setViewName("views/storage/prepay.html");
        	mv.addObject("info",responseInfo);
        	
        	
        	return mv;
		} catch (MedicineServiceException e) {
			System.out.println("deleteMedicineCostController错误");
			e.printStackTrace();
			
        	ResponseInfo responseInfo = new ResponseInfo();
        	responseInfo.setMsg("deleteMedicineCostController服务方法错误");
        	responseInfo.setState(500);
        	ModelAndView mv = new ModelAndView();
        	mv.setViewName("views/storage/prepay.html");
        	mv.addObject("info",responseInfo);
        	
        	
        	return mv;
		} catch (NullPointerException e) {
			System.out.println("不完整错误");
			e.printStackTrace();
			
        	ResponseInfo responseInfo = new ResponseInfo();
        	responseInfo.setMsg("请输入全部的数据");
        	responseInfo.setState(500);
        	ModelAndView mv = new ModelAndView();
        	mv.setViewName("views/storage/prepay.html");
        	mv.addObject("info",responseInfo);
        	
        	
        	return mv;
		} catch (Exception e) {
			System.out.println("其他错误");
			e.printStackTrace();
			
        	ResponseInfo responseInfo = new ResponseInfo();
        	responseInfo.setMsg("其他错误");
        	responseInfo.setState(500);
        	ModelAndView mv = new ModelAndView();
        	mv.setViewName("views/pharmacy/medimodify.htm");
        	mv.addObject("info",responseInfo);
        	
        	
        	return mv;
		}
    }
    
    /**
     * @param req
     * @return
     * 查询药物的controller方法
     */
    @RequestMapping("findMedicineCost")
    @ResponseBody
    public ResponseInfo findMedicineCostByNameController(HttpServletRequest req) {
    	
    	String mediname = req.getParameter("mediname");
    	//System.out.println(mediname); 
    	MedicineCost medicineCost = new MedicineCost();
    	medicineCost.setMediName(mediname);
		
    	try {
    		List<MedicineCost> medis = medicineCostService.findMedicineCostByName(medicineCost);
        	
        	ResponseInfo responseInfo = new ResponseInfo();
        	responseInfo.setData(medis);
        	responseInfo.setMsg("查询药物");
        	responseInfo.setState(200);
        	
        	return responseInfo;
		} catch (MedicineServiceException e) {
			System.out.println("findMedicineCostByNameController错误");
			e.printStackTrace();
			
        	ResponseInfo responseInfo = new ResponseInfo();
        	responseInfo.setMsg("findMedicineCostByNameController服务方法错误");
        	responseInfo.setState(500);
        	return responseInfo;
		}  catch (Exception e) {
			System.out.println("其他错误");
			e.printStackTrace();
			
        	ResponseInfo responseInfo = new ResponseInfo();
        	responseInfo.setMsg("其他错误");
        	responseInfo.setState(500);
        	return responseInfo;
		}
    }


    
	
}
